package com.nbcio.modules.crm.opportunity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.crm.opportunity.domain.CrmOpportunity;
import com.nbcio.modules.crm.opportunity.service.ICrmOpportunityService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * CRM销售机会管理控制器
 */
@Tag(name = "CRM销售机会管理")
@RestController
@RequestMapping("/crm/opportunity")
@RequiredArgsConstructor
public class CrmOpportunityController {

    private final ICrmOpportunityService opportunityService;

    /**
     * 分页查询销售机会
     */
    @GetMapping("/page")
    public R<IPage<CrmOpportunity>> page(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String opportunityCode,
            @RequestParam(required = false) String opportunityName,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) Long ownerUserId,
            @RequestParam(required = false) String stage,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String source,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        LambdaQueryWrapper<CrmOpportunity> qw = new LambdaQueryWrapper<>();
        if (opportunityCode != null && !opportunityCode.isEmpty()) {
            qw.like(CrmOpportunity::getOpportunityCode, opportunityCode);
        }
        if (opportunityName != null && !opportunityName.isEmpty()) {
            qw.like(CrmOpportunity::getOpportunityName, opportunityName);
        }
        if (customerId != null) {
            qw.eq(CrmOpportunity::getCustomerId, customerId);
        }
        if (ownerUserId != null) {
            qw.eq(CrmOpportunity::getOwnerUserId, ownerUserId);
        }
        if (stage != null && !stage.isEmpty()) {
            qw.eq(CrmOpportunity::getStage, stage);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq(CrmOpportunity::getStatus, status);
        }
        if (source != null && !source.isEmpty()) {
            qw.eq(CrmOpportunity::getSource, source);
        }
        if (startTime != null && endTime != null && !startTime.isEmpty() && !endTime.isEmpty()) {
            qw.between(CrmOpportunity::getCreateTime, startTime, endTime);
        }
        qw.orderByDesc(CrmOpportunity::getCreateTime);
        IPage<CrmOpportunity> page = opportunityService.page(Page.of(pageNum, pageSize), qw);
        return R.ok(page);
    }

    /**
     * 根据ID查询销售机会
     */
    @GetMapping("/{id}")
    public R<CrmOpportunity> get(@PathVariable("id") String id) {
        CrmOpportunity opportunity = opportunityService.getById(Long.parseLong(id));
        return R.ok(opportunity);
    }

    /**
     * 新增销售机会
     */
    @PostMapping
    public R<Boolean> create(@RequestBody CrmOpportunity opportunity) {
        return R.ok(opportunityService.save(opportunity));
    }

    /**
     * 更新销售机会
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") String id, @RequestBody CrmOpportunity opportunity) {
        opportunity.setOpportunityId(Long.parseLong(id));
        return R.ok(opportunityService.updateById(opportunity));
    }

    /**
     * 删除销售机会
     */
    @DeleteMapping("/{id}")
    public R<Boolean> remove(@PathVariable("id") String id) {
        return R.ok(opportunityService.removeById(Long.parseLong(id)));
    }

    /**
     * 批量删除销售机会
     */
    @PostMapping("/batch-delete")
    public R<Boolean> batchDelete(@RequestBody List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return R.ok(opportunityService.removeByIds(longIds));
    }
}


